﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
/// <summary>
/// Summary description for FileUploadControl
/// </summary>
namespace WB.Library
{
    public class EnumsFile
    {
        public const string Files = "/userfiles";
        public const string Images = "/userfiles/images";
        public const string Catalogs = "/userfiles/images/Catalogs";
        public const string Thumbnails = "/userfiles/images/Thumbnails";
        public const string Advert = "/userfiles/adverts";
        public const string Videos = "/userfiles/videos";
        public const string Banner = "/userfiles/banner";
        public const string Player = "/player";
        public const string Document = "/userfiles/Document";
        public const string DocumentImage = "/userfiles/DocumentImage";
        public const string Code = "/userfiles/Code";
    }

    public class FileUploadControl
    {
        public static string GetFileName(string FileName, string Absolute, string TextId, int MaxLength)
        {
            string file = "";
            string path = "";

            string ext = "";
            string nor = "";

            int len = FileName.Length;
            int index = FileName.LastIndexOf(".");

            ext = FileName.Substring(index, len - index);
            nor = FileName.Substring(0, index);

            if (string.IsNullOrEmpty(TextId))
            {
                TextId = UnicodeConversion.CreateInstant().GetStringId(nor);
            }

            file = HttpContext.Current.Server.MapPath("~") + Absolute + "/" + TextId + ext;

            string subname = "-" + DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss").Replace("-", "");
            path = Absolute + "/" + TextId + ext;
            if (File.Exists(file))
            {
                path = Absolute + "/" + TextId + subname;
                if (path.Length >= MaxLength + 4)
                    path = path.Substring(0, path.Length);
                path += ext;
            }

            return path;
        }

        public static string FullPath(FileUpload fileUpload, string FileName, int MaxLength, string AbsolutePath)
        {
            string textId = "";
            if (!string.IsNullOrEmpty(FileName))
                textId = UnicodeConversion.CreateInstant().GetStringId(FileName);
            return FullPath(fileUpload, AbsolutePath, textId, MaxLength);
        }

        public static string FullPath(FileUpload fileUpload, int MaxLength)
        {
            return FullPath(fileUpload, EnumsFile.Files, "", MaxLength);
        }

        public static string FullPath(FileUpload fileUpload, string AbsolutePath, string TextId, int MaxLength)
        {
            string FullPath = "";
            if (string.IsNullOrEmpty(AbsolutePath))
                AbsolutePath = EnumsFile.Files;

            if (fileUpload.HasFile)
            {
                FullPath = GetFileName(fileUpload.FileName, AbsolutePath, TextId, MaxLength);
                fileUpload.SaveAs(HttpContext.Current.Server.MapPath("~") + FullPath);
            }

            return FullPath;
        }
    }
}